<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="../d3-tip.css" />
    <style>
     
    </style>
  </head>
  <body>


    <script src="../d3.js"></script>
    <script src="../d3-v6-tip.js"></script>
    <script>

      const width = 900 ;
      const height = 900 ;
      const svg = d3
        .select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", height)
        .style("border", "1px solid #ccc");


      let data ;
      let root ;
      (async function(){
        data = await d3.json('data/china_province_city.json')   
          
        root = d3.hierarchy(data,d=>d.area);

        const tree = d3.tree()
          .size([width-150,height-150])

        tree(root)  


        const g = svg.append('g').attr('transform','translate(50,50)');

        g.selectAll('circle')
          .data(root.descendants())
          .join('circle')
          .attr('cy',d=>d.x)
          .attr('cx',d=>d.y)
          .attr('r',4)
          .attr('fill',d=>d.children?'#666':'#ccc');


        const link = d3.link(d3.curveBumpX)
          .y(d=>d.x)
          .x(d=>d.y)

        g.selectAll('path')
          .data(root.links())
          .join('path')
          .attr('d',link)
          .attr('fill','none')
          .attr('stroke','#666')

        g.selectAll('text')
          .data(root.descendants())
          .join('text')
          .attr('x',d=>d.children?d.y-10:d.y+10)
          .attr('y',d=>d.x)
          .text(d=>d.data.name)
          .attr('font-size',10)  
          .attr('text-anchor',d=>d.children?'end':'start')
      })();

      
    </script>
  </body>
</html>
